package org.codingeasy.hotbean;


import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

/**
* 字节码类加载器  
* @author : KangNing Hu
 * @since 2.0
*/
public class HotClassLoader extends ClassLoader {

  private byte[] bytes;

  private static final String CUSTOMIZER = "Customizer";

  public HotClassLoader(byte[] bytes){
    Assert.notNull(bytes , "bytes is not null");
    this.bytes = bytes;
  }


  @Override
  protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
    /**
     * 解决 bean info 重复加载问题
     */
    if (name.endsWith(CUSTOMIZER)){
      name = StringUtils.substringBeforeLast(name ,CUSTOMIZER );
    }
    //加载class
    return super.loadClass(name, resolve);
  }

  @Override
  protected Class<?> findClass(String name) throws ClassNotFoundException {
    return defineClass(name, bytes, 0, bytes.length);
  }


}
